package cn.infinite.security.configuration;

import cn.infinite.security.filter.SmsAuthenticationFilter;
import lombok.AllArgsConstructor;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.DefaultSecurityFilterChain;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.csrf.CsrfFilter;

/**
 * 短信登录配置
 */
@AllArgsConstructor
public class SmsAuthenticationConfigurerAdapter extends SecurityConfigurerAdapter<DefaultSecurityFilterChain,
        HttpSecurity> {

    private SmsAuthenticationFilter smsAuthenticationFilter;

    private AuthenticationProvider authenticationProvider;

    @Override
    public void configure(HttpSecurity builder) throws Exception {
        this.smsAuthenticationFilter.setAuthenticationManager(builder.getSharedObject(AuthenticationManager.class));
        builder.authenticationProvider(this.authenticationProvider).addFilterAfter(this.smsAuthenticationFilter,
                CsrfFilter.class);
    }
}
